CmdUtils.CreateCommand({
  names: ["say hello", "greet"],
  preview: "Displays a <i>salutary</i> greeting to the planet.",
  execute: function hello_execute() {
    displayMessage(_("Hello, World!"));
  }
})

CmdUtils.CreateCommand({
  names: ["insert date"],
  homepage: "http://azarask.in/",
  author: { name: "Aza Raskin", email: "aza@mozilla.com"},
  contributors: ["Atul Varma"],
  license: "MPL",
  description: "Inserts today's date.",
  help: "If you're in an editable text area, inserts today's date, formatted for the current locale.",
  
  _date: function date__date() {
    return new Date().toLocaleDateString();
  },
  
  preview: function date_preview(pblock) {
    var msg = _('Inserts today\'s date: "<i>${date}</i>"');
    pblock.innerHTML = CmdUtils.renderTemplate(msg, {date: this._date()});
  },
  
  execute: function date_execute() {
    CmdUtils.setSelection(this._date());
  }
})

CmdUtils.CreateCommand({
  names: ["map me"],
  
  _getMapUrl: function() {
    var loc = CmdUtils.getGeoLocation();
    var mapUrl = "http://maps.google.com/staticmap?";

    var params = {
      center: loc.lat + "," + loc.long,
      size: "500x400",
      zoom: 14,
      key: "ABQIAAAAGZ11mh1LzgQ8-8LRW3wEShQeSuJunOpTb3RsLsk00-MAdzxmXhQoiCd940lo0KlfQM5PeNYEPLW-3w"
    };

    return mapUrl + jQuery.param( params );
  },
  
  preview: function( pblock ) {
    var msg = "Inserts a map of your current location: <br/>" +
              "<img src='${url}'/>";
    pblock.innerHTML = _(msg, {url: this._getMapUrl()});
  },
  
  execute: function( ) {
    CmdUtils.getImageSnapshot( this._getMapUrl(), function(imgData) {
      CmdUtils.setSelection( "<img src='" + imgData +"'/>");
    })
  }
})

CmdUtils.CreateCommand({
  names: ["echo"],
  arguments: [{role: "object",
               nountype: noun_arb_text,
               label: "your shout"}],
  preview: function( pblock, arguments ) {
    pblock.innerHTML = _("Will echo: ") + arguments.object.text;
  },
  execute: function( arguments ) {
    var msg = arguments.object.text + "... " + arguments.object.text + "......";
    displayMessage( msg );
  }
});

var noun_type_freebase_topic = {
  name: "Freebase topic",
  label: "topic",
  suggest: function freebase_topic_suggest(text, html, callback) {
    return [
      CmdUtils.makeSugg(text),
      $.ajax({
        url: "http://www.freebase.com/api/service/search",
        dataType: "json",
        data: {prefix: text, limit: 5},
        success: function suggestTopics(response) {
          if (response.status === "200 OK")
            callback([CmdUtils.makeSugg(result.name, result.name, result)
                      for each (result in response.result)]);
        },
      })];
  }
};

CmdUtils.CreateCommand({
  names: ["lookup on freebase"],
  arguments: {object: noun_type_freebase_topic},
  preview: function preview(pblock, args) {
    pblock.innerHTML = _(("Go to the Freebase topic page for " +
                          "{if text}${text}{else}any topic{/if}."),
                         args.object);
  },
  execute: function goToFreebase({object: {text, data}}) {
    Utils.openUrlInBrowser(
      text
      ? (data
         ? "http://www.freebase.com/view" + data.id
         : ("http://www.freebase.com/search?query=" +
            encodeURIComponent(text)))
      : "http://www.freebase.com/");
  }
});

var gId = 42; // used for avoiding needless preview updates when typing
CmdUtils.CreateCommand({
  name: "memo",
  description: "Lets you jot a memo for the page.",
  author: "satyr",
  argument: noun_arb_text,
  execute: function memo_execute({object: {html}}) {
    var {href} = CmdUtils.getDocument().location;
    var list = Bin[href]() || [];
    list.push(html);
    Bin[href](list);
    gId = +new Date;
  },
  preview: function memo_preview(pb) {
    var {href} = CmdUtils.getDocument().location;
    var list = Bin[href]();
    if (!list) {
      pb.innerHTML =
        <>No memos taken for: <small><code>{href}</code></small></>;
      return;
    }
    if (pb.ownerDocument.getElementById(gId)) return;
    var ol = CmdUtils.previewList(pb, list, function deleteMemo(i, ev) {
      $(ev.target).closest("li").slideUp();
      list.splice(i, 1);
      Bin[href](list.length ? list : null);
      gId = +new Date;
    });
    ol.id = gId;
  },
});

CmdUtils.CreateCommand({
  name: "list memos",
  description: "Opens all memos you've taken in a new tab.",
  author: "satyr",
  execute: function list_memos_execute() {
    var htm = +Bin + " page(s)<dl>", {escapeHtml} = Utils;
    for (var [url, memos] in Bin) {
      htm += "<dt>" + escapeHtml(url) + "</dt><dd><ol>";
      for each (var memo in memos) htm += "<li>" + memo + "</li>";
      htm += "</ol></dd>";
    }
    Utils.openUrlInBrowser("data:text/html," + encodeURI(htm + "</dl>"));
  },
});